﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataCentral.UsecaseControllers;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using DataCentral.Forms;
using System.IO;
using System.Reflection;

namespace DataCentral
{
    static class Program
    {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        private static LoginUsecase ucLogin;

        
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                //SBOParseHTML form = new SBOParseHTML();
                //form.ShowDialog();

                bool createdNew = true;
                using (Mutex mutex = new Mutex(true, "DataCentral", out createdNew))
                    if (createdNew)
                    {
                        DevExpress.UserSkins.OfficeSkins.Register();
                        DevExpress.UserSkins.BonusSkins.Register();
                        DevExpress.Skins.SkinManager.EnableFormSkins();
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);


                        Thread.CurrentThread.Name = "MainThread";
                        if (Properties.Settings.Default.APP_IsFirstUse)
                        {
                            SQLConnectionForm frmSQLConnection = new SQLConnectionForm();
                            frmSQLConnection.ShowDialog();
                        }


                        ucLogin = new LoginUsecase();
                        ucLogin.StartUsecase();

                        //try
                        //{
                        Application.Run();
                        //}
                        //catch (Exception e)
                        //{
                        //    throw e;
                        //}
                    }
                    else
                    {
                        Process current = Process.GetCurrentProcess();
                        foreach (Process process in Process.GetProcessesByName(current.ProcessName))
                        {
                            if (process.Id != current.Id)
                            {
                                SetForegroundWindow(process.MainWindowHandle);
                                break;
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                WriteErrorToLog(ex.Message, "Error.txt");
            }
        }

        private static object myobject = new object();

        static void ucLogin_RestartApplication(object sender, EventArgs e)
        {
            
        }

        static void RestartApplication()
        {
            lock (myobject)
            {
                string a = Thread.CurrentThread.Name;
                ucLogin._FrmLogin.frmMain.Close();
                ucLogin._FrmLogin.Close();
                Main();
            }
        }

        private static void WriteErrorToLog(string errorText, string fileName)
        {
            string applicationFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string fullPath = applicationFolder + "\\" + fileName;
            if (File.Exists(fullPath))
            {
                using (StreamWriter swOutfile = new StreamWriter(fullPath, true))
                {
                    swOutfile.WriteLine(DateTime.Now.ToString());
                    swOutfile.WriteLine(errorText);
                }
            }
            else
            {
                using (StreamWriter swOutfile = new StreamWriter(fullPath))
                {
                    swOutfile.WriteLine(DateTime.Now.ToString());
                    swOutfile.Write(errorText);
                }
            }
        }        
    }
}
